﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace GenericHostSample
{
    public class MyServiceA : BackgroundService
    {
        private readonly MyModule _module;

        private readonly ILogger<MyServiceA> _logger;

        public MyServiceA(MyModule module, ILogger<MyServiceA> logger)
        {
            _module = module;
            _logger = logger;

            _logger.LogInformation("哈哈，注入成功了");
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("MyServiceA is starting.");

            stoppingToken.Register(() => _logger.LogInformation("MyServiceA is stopping."));

            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("MyServiceA is doing background work.");

                await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
            }

            _logger.LogInformation("MyServiceA background task is stopping.");
        }
    }
}